import apiMod from '@/apiMod'
import { ElMessageBox, ElMessage } from 'element-plus'
import { onDownloadFile } from '@/utils/util'
import { useCommonStore } from '@/stores/common'
const { dictList } = storeToRefs(useCommonStore())

let reloadLog = ref(0)
export const useOperate = (dataType?: string | number) => {
	const onDelete = (query: any, api: string, tip: string) => {
		return new Promise((resolve, reject) => {
			ElMessageBox.confirm(`请确实是否要删除${tip}的数据?`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					if (apiMod[api]) {
						apiMod[api](Array.isArray(query) ? query : [query]).then(() => {
							ElMessage({
								type: 'success',
								message: '删除成功',
							})
							resolve('ok')
						})
					} else {
						ElMessage({
							type: 'warning',
							message: '请检查请求接口地址是否正确',
						})
					}
				})
				.catch((err) => {
					reject(err)
				})
		})
	}

	const onMessageChange = () => {
		return new Promise(async (resolve, reject) => {
			//1: '养老服务对象',
			// 2: '养老服务机构',
			// 3: '社区养老服务设施',
			// 4: '养老服务组织',
			// 5: '养老从业人员',
			// 6: '志愿者',
			// 7: '养老专家',
			// 8: '服务商',
			const msgTipObj = {
				1: [
					h('p', null, '(1)只能变更"基础信息","扩展信息","其他信息"'),
					h('p', null, '(2)变更将提交服务对象所在街道审核，通过后方可变更'),
					h('p', null, '(3)变更后将通知该服务对象数据录入方'),
				],
				2: [
					h('p', null, '(1)只能变更"基础信息","扩展信息","其他信息"'),
					h('p', null, '(2)变更将提交养老服务组织所在区，通过后方可变更'),
					h('p', null, '(3)变更后将通知该养老服务组织数据录入方'),
				],
				3: [
					h('p', null, '(1)只能变更“基础信息”“扩展信息”“其他信息”“运营记录信息”'),
					h('p', null, '(2)变更将提交养老服务设施所在区，通过后方可变更'),
					h('p', null, '(3)变更后将通知该养老服务设施数据录入方'),
				],
				4: [
					h('p', null, '(1)只能变更"基础信息","扩展信息"'),
					h('p', null, '(2)变更将提交养老服务组织所在区，通过后方可变更'),
					h('p', null, '(3)变更后将通知该养老服务组织数据录入方'),
				],
				5: [
					h('p', null, '(1)只能变更"基础信息","扩展信息"'),
					h('p', null, '(2)变更将提交养老从业人员原所属单位，通过后方可变更'),
					h('p', null, '(3)变更后将通知该养老从业人员数据录入方'),
				],
				6: [
					h('p', null, '(1)只能变更"基础信息","扩展信息","其他信息"'),
					h('p', null, '(2)变更将提交服务对象所在街道审核，通过后方可变更'),
					h('p', null, '(3)变更后将通知该服务对象数据录入方'),
				],
				7: [
					h('p', null, '(1)只能变更"基础信息"'),
					h('p', null, '(2)变更将提交养老专家所在街道审核，通过后方可变更'),
					h('p', null, '(3)变更后将通知该养老专家数据录入方'),
				],
				8: [
					h('p', null, '(1)只能变更"基础信息","扩展信息","其他信息"'),
					h('p', null, '(2)变更将提交服务对象所在街道审核，通过后方可变更'),
					h('p', null, '(3)变更后将通知该服务对象数据录入方'),
				],
			}
			ElMessageBox({
				title: '信息变更注意事项',
				message: h('div', null, [...msgTipObj[dataType], h('p', { style: 'margin-top: 10px' }, '是否确认要继续进行信息变更？')]),
				showCancelButton: true,
				confirmButtonText: '信息变更',
				cancelButtonText: '取消',
			})
				.then(() => {
					resolve('ok')
				})
				.catch(() => {})
		})
	}

	const getDictName = (code, value) => {
		const data = Array.isArray(dictList.value) ? dictList.value : dictList.value[code] || []
		let nameStr = ''
		if (Array.isArray(data) && value) {
			const filterStr = Array.isArray(value) ? value : value.toString().split(',')
			const v = data
				.map((item) => (filterStr.includes(item.value + '') ? item.label : ''))
				.filter(Boolean)
				.join(',')
			nameStr = v
		}
		return nameStr
	}
	const downloadFile = (api: string, query: any) => {
		apiMod[api](query)
			.then((res) => {
				if (res.data) {
					onDownloadFile(res.data)
				}
			})
			.catch((res) => {})
	}

	const getValue = (value, item) => {
		let tempName = value
		value = (value && value.toString().split(',')) || []
		if (item.dicData) {
			tempName = item.dicData
				.map((item) => {
					if (value.includes(item.value + '') || value.includes(item.label + '')) {
						return item.label
					}
				})
				.filter(Boolean)
				.join(',')
		} else if (item.code) {
			const dictName = getDictName(item.code, value)
			tempName = dictName || value.join(',')
		}
		return tempName
	}

	// 变更数据的比对
	const getContent = (before, after, code: number, changeType: string, params: any, rowKey = 'id') => {
		let changeRes = []
		// 处理已存在的数据
		let beforeIds = []
		if (!before) before = []
		if (!after) after = []
		if (Array.isArray(before) && before.length > 0) {
			before.map((item) => {
				const tempAfter = after.filter((chi) => chi[rowKey] == item[rowKey])[0] || { ...item, delFlag: 1 }
				beforeIds.push(item[rowKey])
				const tempObj = {
					changeBefore: {
						...item,
						...params,
					},
					changeAfter: {
						...tempAfter,
						...params,
					},
					changeData: {
						...tempAfter,
						...params,
					},
					code,
					changeType,
				}
				changeRes.push(tempObj)
			})
		}
		if (Array.isArray(after) && after.length > 0) {
			const afterData = after.filter((item) => !beforeIds.includes(item[rowKey]))
			afterData.forEach((item) => {
				const tempObj = {
					changeBefore: {},
					changeAfter: {
						...item,
						...params,
					},
					changeData: {
						...item,
						...params,
					},
					code,
					changeType,
				}
				changeRes.push(tempObj)
			})
		}
		return changeRes
	}

	return {
		onDelete,
		getValue,
		getDictName,
		onMessageChange,
		downloadFile,
		getContent,
	}
}

export const useConfig = () => {
	const getFieldData = (tableName: string, versionNumber?: any) => {
		return new Promise((resolve, reject) => {
			apiMod.view_metadata_fields({ tableName: tableName, versionNumber }).then((res) => {
				if (res.data) {
					let FieldColumn = Object.keys(res.data).map((key, index) => {
						return {
							width: 220,
							label: res.data[key],
							prop: key,
						}
					})
					resolve(FieldColumn)
				}
			})
		})
	}

	return {
		reloadLog,
		getFieldData,
	}
}
